#include "fsstorage.h"

#include "fspersistencecommon.h"

#include <ipersistentdocument.h>
#include <mimetype.h>

#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QObject>
#include <QtCore/QUrl>
#include <QtGui/QDesktopServices>
#include <QtGui/QFileDialog>

using namespace core;
using namespace fspersistence;

QByteArray FsStorage::read(const QSharedPointer<IPersistentDocument>&
    document) const
{
    QByteArray ret;

    const QUrl url = document->url();
    const QString path = url.toLocalFile();

    QFile file(path);

    if (file.exists())
    {
        if (file.open(QIODevice::ReadOnly))
        {
            ret = file.readAll();
            file.close();
        }
    }
    else
    {
        Q_ASSERT(false); // doesn't exists
    }

    return ret;
}

void FsStorage::write(const QSharedPointer<IPersistentDocument>&
    document) const
{
    const QUrl url = document->url();
    const QString path = url.toLocalFile();

    QFile file(path);

    if (file.exists())
    {
        if (file.open(QIODevice::WriteOnly))
        {
            file.write(*document->serializedData());
            file.flush();

            file.close();
        }
    }
    else
    {
        Q_ASSERT(false); // doesn't exists
    }
}

bool FsStorage::create(
    const QUrl& url) const
{
    bool ret = false;

    const QString path = url.toLocalFile();

    QFile file(path);
    QFileInfo fileInfo(path);

    bool status = QDir().mkpath(fileInfo.absolutePath());

    if (status)
    {
        status = file.open(QIODevice::ReadWrite);

        if (status)
        {
            ret = true;
            file.close();
        }
    }

    return ret;
}

bool FsStorage::erase(const QSharedPointer<IPersistentDocument>&
    document) const
{
    bool ret = false;

    const QString path = document->url().toLocalFile();

    QFile file(path);

    if (file.exists())
    {
        ret = file.remove(path);
    }

    return ret;
}

bool FsStorage::exists(const QUrl& url) const
{
    bool ret = false;

    const QString path = url.toLocalFile();

    QFileInfo fileInfo(path);
    ret = fileInfo.exists();

    return ret;
}

QString FsStorage::registeredName() const
{
    return FILE_SYSTEM_STORAGE;
}

QString FsStorage::name() const
{
    return QObject::tr("File System");
}

QUrl FsStorage::urlForSave(const QString& proposedName,
    const core::MimeType& mimeType, bool& canceled) const
{
    QUrl ret;

    const QString home = QString("%1/%2.%3").arg(QDir::homePath()).
        arg(proposedName).arg(mimeType.suffixes().first());
    const QString fileName = QFileDialog::getSaveFileName(0,
            QObject::tr("Save document"), home,
            mimeType.filterString());

    canceled = fileName.isEmpty();

    ret = QUrl::fromLocalFile(fileName);

    return ret;
}

QUrl FsStorage::urlForOpen(const core::MimeType& mimeType) const
{
    QUrl ret;

    const QString fileName = QFileDialog::getOpenFileName(0,
            QObject::tr("Open document"), QDir::homePath(),
            mimeType.filterString());

    ret = QUrl::fromLocalFile(fileName);

    return ret;
}

bool FsStorage::operator==(const IStorage& storage) const
{
    const FsStorage* _storage = dynamic_cast<const FsStorage*>(&storage);

    return _storage != 0;
}

IStorage* FsStorage::copy() const
{
    return new FsStorage();
}

